{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "keras_01.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "private_outputs": true,
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yejianfeng2014/AI/blob/master/keras_01.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "mjtWuzCUCHcZ",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import tensorflow as tf \n",
        "tf.__version__"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "UF1XzyEFCRyB",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# 首先，我们人为地造一组由 y = 0.5x + 2 加上一些噪声而生成的数据，数据量一共有200个，其中前160作为train set，后40作为test set。"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "xHM6RR7NDOpq",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        " \n",
        "X = np.linspace(-2, 6, 200)\n",
        "np.random.shuffle(X)\n",
        "Y = 0.5 * X + 2 + 0.15 * np.random.randn(200,) \n",
        " \n",
        "# plot data\n",
        "plt.scatter(X, Y)\n",
        "plt.show()\n",
        " \n",
        "X_train, Y_train = X[:160], Y[:160]     # train first 160 data points\n",
        "X_test, Y_test = X[160:], Y[160:]       # test remaining 40 data points\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "yufqqyJ1DqgI",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from keras.models import Sequential\n",
        "from keras.layers import Dense\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "-K2mpxNXEhQN",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "其中Sequential是Keras中构建NN最常用的一种Model（也是最简单的一种），一个Sequential的Model 就是 a linear stack of layers，也就是说，你只要按顺序（使用add()方法）一层一层地顺序地添加神经网络层就可以了。而Dense表示全连接层，此时它需要接收两个参数，即输入的节点数及输出的节点数，特别地，在一层一层地构建NN时，Keras还可以根据上一层的输出来推断下一次的输入，所以有些全连接层参数可以省略。\n"
      ]
    },
    {
      "metadata": {
        "id": "u-kZ5BKyEc4i",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# 因为输入和输出都是一个元素\n",
        "model = Sequential()\n",
        "model.add(Dense(output_dim = 1, input_dim = 1))\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "l-TvK7zJE4yz",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "接下来执行构建模型的第一步，即Model compilation。这一步是要指定模型中的loss function（在这例子中使用的是最小二乘误差‘mse’），优化器以及metrics等内容。优化器你可以使用系统提供的默认优化器，例如你可以像下面这样用'sgd'表示随机梯度下降。"
      ]
    },
    {
      "metadata": {
        "id": "CDLVwyYWE6dj",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# 方式一：\n",
        "# model.compile(loss = 'mse',optimer = 'sgd')\n",
        "# 方式二 \n",
        "from keras.optimizers import SGD\n",
        "model.compile(loss='mse', optimizer=SGD(lr=0.01, momentum=0.9, nesterov=True))\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "KCQixEjOFhPS",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "然后来执行Training的部分。在这一步你可以有两个选择。第一种直接使用fit，这个简直和Scikit-learn中的感觉像极了！你只要在fit方法的参数列表中指定训练数据（特征向量和label）、训练的次数和用来做梯度下降的batch size就可以了\n",
        "\n",
        "另外一个选择是你也可以采用下面的语法来feed batches to your model manually："
      ]
    },
    {
      "metadata": {
        "id": "fq_e8K28FiP3",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "# 方式一 \n",
        "model.fit(X_train,Y_train,epochs = 10 ,batch_size  = 64)\n",
        "# 方式二：\n",
        "# model.train_on_batch(x_batch, y_batch)\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "ylOhBwAzGI4E",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "print('Training -----------')\n",
        "for step in range(100):\n",
        "    cost = model.train_on_batch(X_train, Y_train)\n",
        "    if step % 20 == 0:\n",
        "        print('train cost: ', cost)\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "2-dXi8TyHJ4x",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "cost = model.evaluate(X_test, Y_test, batch_size=40)\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "he9nUoWOHMDh",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "print('\\nTesting ------------')\n",
        "loss_and_metrics = model.evaluate(X_test, Y_test, batch_size=40)\n",
        "print('test cost:', cost)\n",
        "W, b = model.layers[0].get_weights()\n",
        "print('Weights=', W, '\\nbiases=', b)\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "Rba_7A20HP09",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "Y_pred = model.predict(X_test)\n",
        "plt.scatter(X_test, Y_test)\n",
        "plt.plot(X_test, Y_pred)\n",
        "plt.show()\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "8s_IBRrsHaZt",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}